﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace Aspfm.Main
{
    public class UserIdentity : IUser<int>
    {
        string _SysRoleLevel = "-1";

        public string _uguid { get; set; }
        public string _uno { get; set; }
        public string _uname { get;  set; }
        public string _dept { get; set; }
        /// <summary>
        /// SysRoleLevel表示该用户对站点文件及文件夹的访问级别。
        /// </summary>
        public string SysRoleLevel
        {
            get
            {
                if (string.IsNullOrEmpty(_SysRoleLevel))
                {
                    _SysRoleLevel = "-1";
                }
                return _SysRoleLevel;
            }
            set
            {
                _SysRoleLevel = value;
            }
        }
        public string LoginGuid { get; private set; }
        public string _loginType { get; set; }

        public void SetLoginGuid()
        {
            LoginGuid = DateTime.Now.ToString("yyyyMMddHHmmss") + Guid.NewGuid().ToString().Substring(0, 8).ToUpper();
        }
        public bool CheckLogin()
        {
            if (string.IsNullOrEmpty(_uno))
            {
                return false;
            }
            return true;
        }

        public UserIdentity(bool GetCurrentUser = true)
        {
            if (GetCurrentUser)
            {
                IPrincipal principal = System.Threading.Thread.CurrentPrincipal;
                if (principal != null)
                {
                    if (!string.IsNullOrEmpty(principal.Identity.Name))
                    {
                        string[] strs = principal.Identity.Name.Split(SessionTool.splitChar);
                        if (strs.Length == 7)
                        {
                            _uguid = strs[0];
                            _uno = strs[1];
                            _uname = strs[2];
                            _dept = strs[3];
                            SysRoleLevel = strs[4];
                            LoginGuid = strs[5];
                            _loginType = strs[6];
                        }
                    }
                }
            }
        }
        public string GetUserString()
        {
            return _uguid + SessionTool.splitChar +
                _uno + SessionTool.splitChar +
                _uname + SessionTool.splitChar +
                _dept + SessionTool.splitChar +
                SysRoleLevel + SessionTool.splitChar+
                LoginGuid + SessionTool.splitChar +
                _loginType;
        }
        public override string ToString()
        {
            return GetUserString();
        }
    }
    public class UserIdentity<TKey> : IIdentity
    {
        public UserIdentity(IUser<TKey> user)
        {
            if (user != null)
            {
                IsAuthenticated = true;
                Name = user.ToString();
            }
        }

        public string AuthenticationType
        {
            get { return "AspfmUser"; }
        }

        public bool IsAuthenticated { get; private set; }

        public string Name { get; private set; }
    }

    public class UserPrincipal<TKey> : IPrincipal
    {
        public UserPrincipal(UserIdentity<TKey> identity)
        {
            Identity = identity;
        }

        public UserPrincipal(IUser<TKey> user)
            : this(new UserIdentity<TKey>(user))
        {

        }

        /// <summary>
        /// 
        /// </summary>
        public UserIdentity<TKey> Identity { get; private set; }

        IIdentity IPrincipal.Identity
        {
            get { return Identity; }
        }


        bool IPrincipal.IsInRole(string role)
        {
            throw new NotImplementedException();
        }
    }
    public interface IUser<T>
    {
    }
}
